package me.zhanshi123.vipstorage.command.subcommand;

import me.zhanshi123.VipSystem.VipAPI;
import me.zhanshi123.vipstorage.MessageManager;
import me.zhanshi123.vipstorage.util.Database;
import me.zhanshi123.vipstorage.Main;
import me.zhanshi123.vipstorage.util.StorageData;
import me.zhanshi123.vipstorage.command.SubCommand;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class ClaimCommand extends SubCommand {
    public ClaimCommand() {
        super("claim");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MessageManager.getString("consoleForbid"));
            return true;
        }
        Player player = (Player) sender;
        String name = player.getName();
        if (args.length != 2) {
            sender.sendMessage(MessageManager.getString("wrongArg"));
            return true;
        }
        String vipGroup = VipAPI.getInstance().getVipGroup(name);
        if (vipGroup != null) {
            sender.sendMessage(MessageManager.getString("noStoredVip"));
            return true;
        }
        int id;
        try {
            id = Integer.parseInt(args[1]);
        } catch (Exception e) {
            sender.sendMessage(MessageManager.getString("wrongArg"));
            return true;
        }
        StorageData data = Database.getInstance().getData(id);
        if (data == null) {
            sender.sendMessage(MessageManager.getString("dataNotFound"));
            return true;
        }
        Main.getVipAPI().addVipWithoutCommands(name, data.getGroup(), data.getLastGroup(), String.valueOf(data.getLastSeconds()));
        Database.getInstance().removeData(id);
        sender.sendMessage(MessageManager.getString("success"));
        return true;
    }
}
